package ca.ubc.icapture.genapha.forms;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;

import icapture.SQLMgr;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:39:23 AM
 * To change this template use File | Settings | File Templates.
 */
public class FbatContinuousForm extends AnalysisFilesForm
{

    private boolean unPhasedFormat = false;

    public FbatContinuousForm()
    {
        super();
        setPhenotypes(SQLMgr.getPhenotypes(2));
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();

        if (getSelectedPhenotype().length() == 0)
        {
            errors.add("selectedPhenotype", new ActionMessage("error.entry.required"));
        }
        if (getSelectedCohort().length == 0)
        {
            errors.add("selectedCohort", new ActionMessage("error.entry.required"));
        }
        if (getEmail().length() == 0)
        {
            errors.add("email", new ActionMessage("error.entry.required"));
        }
        else if (!getEmail().contains("@"))
        {
            errors.add("email", new ActionMessage("error.entry.invalid.email"));
        }
        if (getSelectedPhenotype().length() > 0 && getSelectedCohort().length > 0)
        {
            //make sure there are phentypes for this cohort
            for (int i = 0; i < getSelectedCohort().length; i++)
            {
                if (SQLMgr.getPhenotypeUsage(Integer.parseInt(getSelectedPhenotype()), Integer.parseInt(getSelectedCohort()[i])) < 1)
                {
                    errors.add("selectedPhenotype", new ActionMessage("error.entry.invalid.phenotype"));
                    break;
                }
            }
        }

        if (isUnPhasedFormat() && (getSelectedGenes() == null || getSelectedGenes().length == 0))// == null || (selectedCohorts[0].toString().equals(" ") && selectedCohorts.length==1))
        {
            errors.add("selectedGenes", new ActionMessage("error.entry.required"));
        }
        return errors;
    }

    /**
     * @return the unPhasedFormat
     */
    public boolean isUnPhasedFormat()
    {
        return unPhasedFormat;
    }

    /**
     * @param unPhasedFormat the unPhasedFormat to set
     */
    public void setUnPhasedFormat(boolean unPhasedFormat)
    {
        this.unPhasedFormat = unPhasedFormat;
    }
}
